package com.zhanghe.springboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhanghe.springboot.model.Student;
import com.zhanghe.springboot.model.Teacher;
import com.zhanghe.springboot.model.TestModel;

@Mapper
public interface testmapper{
	@Select("SELECT id,name FROM TESTTABLE where id=#{id} ")
	 TestModel findByid(@Param("id")String name);
	@Update("update TESTTABLE set name=#{name} where id=#{id}")
	public int updateById(@Param("id")String id,@Param("name")String name);
	
	
	@Select("select s.*,t.id as tid,t.name as tname from student s,teacher t where s.teacher=t.id and s.id=#{id}")
	@Results({ 
		@Result(column="tid", property="teacher.id"),
		@Result(column="tname", property="teacher.name"),
		@Result(property="teacher", column="teacher",  
		one=@One(select="com.zhanghe.springboot.mapper.testmapper.getTeacherById2")
		),  
	})  
	public Student getStdentbyId2(@Param("id")String id);
	
	@Select("select * from teacher t where t.id=#{id}")
	public Teacher getTeacherById2(@Param("id")String id);
	
	@Select("select s.*,t.id as tid,t.name as tname from student s,teacher t where s.teacher=t.id and s.id=#{id}")
	@Results({ 
		@Result(column="tid", property="teacher.id"),
		@Result(column="tname", property="teacher.name")  
	})  
	public Student getStdentbyId(@Param("id")String id);
	
	
	@Select("select * from teacher t where t.id=#{id}")
	@Results({
		@Result(property = "students", column = "id",  
		many = @Many(select = "com.zhanghe.springboot.mapper.testmapper.getTeachersStudent"))  
	})
	public Teacher getTeacherByid(@Param("id")String id);
	
	@Select("select * from student t where t.teacher=#{teacher}")
	public List<Student> getTeachersStudent(@Param("teacher")String teacher);
	
}
